/**
 * Created by aa on 2016/9/5.
 */

var EventUtil=require('./EventUtil.js');
var CheckBrowser=require('./CheckBrowser.js');
function LazyLoad(srcDir,srcName,scrollTop){
    var load=function(){
        var s = document.createElement('script');
        var checkBrowser=new CheckBrowser();
        s.type = 'text/javascript';
        s.async = true;
        if(checkBrowser.getBrowserType()=='IE: 8.0') {
            s.src = srcDir + '/fuckingIE/' + srcName;
        }
        else{
            s.src = srcDir + '/' + srcName;
        }
        var x = document.getElementsByTagName('script')[0];
        x.parentNode.insertBefore(s, x);

        var eventUtil=new EventUtil();
        eventUtil.removeHandler(window,'scroll',lazyHandle);
    };

    var lazyHandle=function(){
        var t = document.documentElement.scrollTop || document.body.scrollTop;
        if( t >= scrollTop ) {
            load();
        }
    };
    this.addLazy=function(){
        if(scrollTop>0) {
            var eventUtil = new EventUtil();
            eventUtil.addHandler(window, 'scroll', lazyHandle);
        }
        else{
            load();
        }
    }
}
module.exports = LazyLoad;